#!/usr/bin/python

import sys
import time
import signal
from multiprocessing import Process
from led import led
import myOut as myMsg
from unsafeLog import unsafeLog

done = False
def signal_handler(signal, frame):
    myMsg.myOut( 'You pressed Ctrl+C!')
    global done
    done = True

signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)

greenLedPin = 22
myMsg.myOut('initializing tempToGoogle')
greenLed = led(greenLedPin)
myMsg.myOut('testing Green LED')
for i in range(5):
    greenLed.on()
    time.sleep(.2)
    greenLed.off()
    time.sleep(.2)


while(not done):
    # Wait before continuing
    myP = Process(target=unsafeLog,args=(0,))
    myP.start() # start it, can only start it once
    myP.join(100) # give it xxx seconds
    greenLed.on()
    time.sleep(4*60)
    greenLed.off()
    
    if myP.is_alive():
        myP.terminate()
        myMsg.myOut( 'Hung Logger!!')
        myP.join()
        
myMsg.myOut('graceful exit from tempToGoogle')
greenLed.off()
sys.exit()
